#include "coroutine.h"
using namespace CCoroutine;
bool PromiseBase::suspend(AwaiterBase *base,int num)
{
    this->awaiter.reset(base);
    if(base->ready())
    {
        return true;
    }
    pos = num;
    m_status = Suspended;
    base->suspend(this);
    return false;
}

AwaiterBase *PromiseBase::final_suspend()
{
    return new SuspendAlways{};
}

PromiseBase::Status PromiseBase::status()
{
    return m_status;
}

void PromiseBase::resume()
{
    m_status = Running;
    run();
    if(m_status == Finished)
    {
        auto a = final_suspend();
        if(!a->ready())
        {
            a->suspend(this);
        }
        delete a;
    }
}


ControllerBase::ControllerBase()
{

}

ControllerBase::ControllerBase(ControllerBase &&o)
{
    this->base = o.base;
    o.base = nullptr;
}

ControllerBase &ControllerBase::operator =(ControllerBase &&o)
{
    this->base = o.base;
    o.base = nullptr;
    return *this;
}

void ControllerBase::set_promise(PromiseBase *p)
{
    this->base = p;
}
